package com.softeem.xyblogmaster.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class PageInfo {
    //当前页
    private long current;
    //每页多少条
    private long size;
    //总记录数
    private long total;
    //总页数
    private long pages;
    //
    private long start;
    private long end;
    //上一页
    private long pre;
    //下一页
    private long next;
    //是不是首页
    private boolean isFirst;
    //是不是尾页
    private boolean isLast;
    //是否有上一页
    private boolean isHasPre;
    //是否有下一页
    private boolean isHasNext;

    private List result;
    //中间的页码数
    private List<Integer> nums=new ArrayList<>();


    public PageInfo(Page page,int displayPages) {
        this.current = page.getCurrent();
        this.result=page.getRecords();
        this.size = page.getSize();
        this.total = page.getTotal();
        this.pages = page.getPages();
        this.start = (current - 1) * size + 1;
        this.end = current * size;
        this.pre = current - 1;
        this.next = current + 1;
        this.isFirst = current == 1;
        this.isLast = current == pages;
        this.isHasPre = current > 1;
        this.isHasNext = current < pages;
        // 计算需要显示的页码范围
        int startPage = (int) Math.max(1, current - (displayPages / 2));
        int endPage = (int) Math.min(pages, startPage + displayPages - 1);

        // 调整起始页码，确保当前页码在中间
        if (endPage - startPage + 1 < displayPages) {
            startPage = Math.max(1, endPage - displayPages + 1);
        }

        for (int i = startPage; i <= endPage; i++) {
            nums.add(i);
        }
    }

}